package com.cskaoyan.service;

import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.param.CouponDetail;
import com.cskaoyan.bean.param.CouponParam;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.mapper.MallCouponMapper;
import com.cskaoyan.mapper.MallCouponUserMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Transactional(rollbackFor = Exception.class)
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    MallCouponMapper mallCouponMapper;

    @Autowired
    MallCouponUserMapper mallCouponUserMapper;



    @Override
    public BaseData<MallCoupon> queryList(CouponParam couponParam) {
        //执行查询之前写一行代码即可
        PageHelper.startPage(couponParam.getPage(),couponParam.getLimit());
        MallCouponExample mallCouponExample = new MallCouponExample();
        MallCouponExample.Criteria criteria = mallCouponExample.createCriteria();
        if(couponParam.getName()!=null){
            criteria.andNameLike("%"+couponParam.getName()+"%");
        }
        if(couponParam.getType()!=null){
            criteria.andTypeEqualTo(couponParam.getType());
        }
        if(couponParam.getStatus()!=null){
            criteria.andStatusEqualTo(couponParam.getStatus());
        }
        criteria.andDeletedEqualTo(false);
        mallCouponExample.setOrderByClause(couponParam.getSort()+" "+couponParam.getOrder());
        List<MallCoupon> mallCoupons = mallCouponMapper.selectByExample(mallCouponExample);
        long total = mallCouponMapper.countByExample(mallCouponExample);
        return BaseData.ok(mallCoupons,total);
    }

    @Override
    public MallCoupon create(MallCoupon mallCoupon) {
//        在mybatis文件中使用useGeneratedKeys="true" keyProperty="id"可以回显id
        int affectedRows = mallCouponMapper.insertSelective(mallCoupon);

        if(affectedRows>0){
            Date date = new Date();
            mallCoupon.setAddTime(date);
            mallCoupon.setUpdateTime(date);
            return mallCoupon;
        }
        return null;
    }

    @Override
    public MallCoupon update(MallCouponUpdate mallCouponUpdate) {
        MallCoupon mallCoupon = new MallCoupon();
        mallCoupon.setUpdateTime(mallCouponUpdate.getUpdateTime());
        mallCoupon.setAddTime(mallCouponUpdate.getAddTime());
        mallCoupon.setCode(mallCouponUpdate.getCode());
        mallCoupon.setDays(mallCouponUpdate.getDays());
        mallCoupon.setDesc(mallCouponUpdate.getDesc());
        mallCoupon.setDeleted(mallCouponUpdate.getDeleted());
        mallCoupon.setDiscount(mallCouponUpdate.getDiscount());
        mallCoupon.setEndTime(mallCouponUpdate.getEndTime());
        mallCoupon.setId(mallCouponUpdate.getId());
        mallCoupon.setGoodsType(mallCouponUpdate.getGoodsType());
        mallCoupon.setGoodsValue(mallCouponUpdate.getGoodsValue());
        mallCoupon.setLimit(mallCouponUpdate.getLimit());
        mallCoupon.setName(mallCouponUpdate.getName());
        mallCoupon.setStartTime(mallCouponUpdate.getStartTime());
        mallCoupon.setStatus(mallCouponUpdate.getStatus());
        mallCoupon.setTag(mallCouponUpdate.getTag());
        mallCoupon.setTotal(mallCouponUpdate.getTotal());
        mallCoupon.setMin(mallCouponUpdate.getMin());
        mallCoupon.setTimeType(mallCouponUpdate.getTimeType());
//        传一个对象按照对象的主键更新
        int affectedRows = mallCouponMapper.updateByPrimaryKey(mallCoupon);
         if(affectedRows>0){
             return mallCoupon;
         }
            return null;
    }

    @Override
    public int delete(MallCoupon mallCoupon) {
        mallCoupon.setDeleted(true);
        int affectedRows = mallCouponMapper.updateByPrimaryKey(mallCoupon);
        if(affectedRows>0){
            return 200;
        }
        return 404;
    }

    @Override
    public BaseData<MallCouponUser> queryUserList(CouponDetail couponDetail) {
        PageHelper.startPage(couponDetail.getPage(),couponDetail.getLimit());
        MallCouponUserExample mallCouponUserExample = new MallCouponUserExample();
        MallCouponUserExample.Criteria criteria = mallCouponUserExample.createCriteria();
        if(couponDetail.getCouponId()!=null){
            criteria.andCouponIdEqualTo(couponDetail.getCouponId());
        }
        if(couponDetail.getUserId()!=null){
            criteria.andUserIdEqualTo(couponDetail.getUserId());
        }
        if(couponDetail.getStatus()!=null){
            criteria.andStatusEqualTo(couponDetail.getStatus());
        }
        mallCouponUserExample.setOrderByClause(couponDetail.getSort()+" "+couponDetail.getOrder());
        List<MallCouponUser> mallCouponUsers = mallCouponUserMapper.selectByExample(mallCouponUserExample);
        long total = mallCouponUserMapper.countByExample(mallCouponUserExample);
        return BaseData.ok(mallCouponUsers,total);

    }

    @Override
    public MallCoupon read(Integer id) {
        MallCoupon mallCoupon = mallCouponMapper.selectByPrimaryKey(id);
        return mallCoupon;
    }


}
